package com.example.telephoneBook;

import com.example.sql.SqlHelp;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity {


	private EditText phoneNumber, password, passwordAgain, name, grade,
			QQNumber, cornet, weixin;
	private Button back, confirmRegister;
	private SqlHelp dbhelp;
	private SQLiteDatabase db;


	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		phoneNumber = (EditText) findViewById(R.id.phoneNumber_edittext);// 电话号码
		password = (EditText) findViewById(R.id.password_edittext);// 密码
		passwordAgain = (EditText) findViewById(R.id.passwordAgain_edittext);// 确认密码
		name = (EditText) findViewById(R.id.name_edittext);// 名字
		grade = (EditText) findViewById(R.id.grade_edittext);// 班级
		QQNumber = (EditText) findViewById(R.id.QQNumber_edittext);// QQ号
		cornet = (EditText) findViewById(R.id.cornet_edittext);// 短号
		weixin = (EditText) findViewById(R.id.weixin_edittext);// 微信号
		back = (Button) findViewById(R.id.back_button);// 返回按钮
		confirmRegister = (Button) findViewById(R.id.confirmRegister_button);// 确认按钮
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
			}
		});
		confirmRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String password_string = password.getText().toString();// 获取密码输入框内容
				String passwordAgain_string = passwordAgain.getText()
						.toString();// 获取确认密码输入框内容
				if (password_string!=""&password_string.equals(passwordAgain_string)) {
					// 数据库插入代码

					dbhelp = new SqlHelp(RegisterActivity.this, SqlHelp.DBName);
					db = dbhelp.getReadableDatabase();
					Cursor cursor = db.query(SqlHelp.TableUser, null,
							"username=?", new String[] { phoneNumber.getText()
									.toString() }, null, null, null);

					int a = cursor.getCount();
					if (a == 0) {
						ContentValues values = new ContentValues();
						values.put("username",
								phoneNumber.getText().toString());
						values.put("password", password.getText().toString());
						values.put("phoneNumber", password.getText().toString());
						ContentValues values2 = new ContentValues();
						values.put("name",
								name.getText().toString());
						values.put("phoneNumber", phoneNumber.getText().toString());
						values.put("shortNumber", cornet.getText().toString());
						values.put("grade", grade.getText().toString());
						values.put("qq", QQNumber.getText().toString());
						values.put("weixin", weixin.getText().toString());
						db.insert(SqlHelp.TableUser, null, values);
						db.insert(SqlHelp.TableNumber, null, values2);
					
					db.close();
					cursor.close();

					Toast.makeText(RegisterActivity.this, "注册成功", 1000).show();
					Intent res2 = new Intent(RegisterActivity.this,
							LoginActivity.class);
					startActivity(res2);}
					else{
						Toast.makeText(RegisterActivity.this, "用户名已存在", 1000).show();
						
					}
				} else {
					Toast.makeText(RegisterActivity.this, "密码不相同", 1000).show();
				}

			}
		});

	}


}
